package eafit.sicom.calculadorapromedios;



import kankan.wheel.widget.OnWheelChangedListener;
import kankan.wheel.widget.OnWheelScrollListener;
import kankan.wheel.widget.WheelView;
import kankan.wheel.widget.adapters.ArrayWheelAdapter;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class WheelGradePercentageActivity extends Activity {

	public static final String GRADE_RESULT_FROM_DIALOG = "GRADE_DIALOG";
	public static final String PERCENTAGE_RESULT_FROM_DIALOG = "PERCENTAGE_DIALOG";
	// Scrolling flag 
	private boolean scrolling = false;

	String i;
	String d;
	String d2;
	String p;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Bundle bundle = getIntent().getExtras();
		Double missingPercentage = (Double) bundle
				.get(MainActivity.MISSING_PERCENTAGE);

		setContentView(R.layout.grade_percentage_dialog_layout);
		if (missingPercentage > 0.0) {

			final String percentages[] = new String[missingPercentage
					.intValue()];
			for (int i = 0; i < missingPercentage.intValue(); i++) {
				percentages[i] = String.valueOf(i + 1) + "%";
			}
			final WheelView percentageWheel = (WheelView) findViewById(R.id.percentage_wheel);
			percentageWheel.setVisibleItems(3);
			ArrayWheelAdapter<String> adapterA = new ArrayWheelAdapter<String>(
					this, percentages);
			percentageWheel.setViewAdapter(adapterA);
			final WheelView integerGrade = (WheelView) findViewById(R.id.grade_integer);
			integerGrade.setVisibleItems(3);
			final String integers[] = new String[] { "0.", "1.", "2.", "3.",
					"4.", "5." };
			ArrayWheelAdapter<String> adapter = new ArrayWheelAdapter<String>(
					this, integers);
			adapter.setTextSize(18);
			integerGrade.setViewAdapter(adapter);

			final String[] digits = { "0", "1", "2", "3", "4", "5", "6", "7",
					"8", "9" };
			String[] zero = { "0" };
			final String decimalsArray[][] = new String[][] { digits, digits,
					digits, digits, digits, zero

			};

			final WheelView decimalGrade = (WheelView) findViewById(R.id.grade_decimals);
			decimalGrade.setVisibleItems(3);
			
			final WheelView decimalGrade2 = (WheelView) findViewById(R.id.grade_decimals2);
			decimalGrade2.setVisibleItems(3);

			integerGrade.addChangingListener(new OnWheelChangedListener() {
				public void onChanged(WheelView wheel, int oldValue,
						int newValue) {
					if (!scrolling) {

						updateDecimals(decimalGrade, decimalsArray, newValue);
						updateDecimals(decimalGrade2, decimalsArray, newValue);
						i = integers[integerGrade.getCurrentItem()];
						d = digits[decimalGrade.getCurrentItem()];
						d2 = digits[decimalGrade2.getCurrentItem()];
						p = percentages[percentageWheel.getCurrentItem()];
					}
				}

			});

			integerGrade.addScrollingListener(new OnWheelScrollListener() {
				public void onScrollingStarted(WheelView wheel) {
					scrolling = true;
				}

				public void onScrollingFinished(WheelView wheel) {
					scrolling = false;
					i = integers[integerGrade.getCurrentItem()];
					updateDecimals(decimalGrade, decimalsArray,
							integerGrade.getCurrentItem());
					updateDecimals(decimalGrade2, decimalsArray,
							integerGrade.getCurrentItem());

				}
			});

			integerGrade.setCurrentItem(3);

			decimalGrade.addChangingListener(new OnWheelChangedListener() {

				@Override
				public void onChanged(WheelView wheel, int oldValue,
						int newValue) {
					d = digits[decimalGrade.getCurrentItem()];

				}
			});

			decimalGrade.addScrollingListener(new OnWheelScrollListener() {

				@Override
				public void onScrollingStarted(WheelView wheel) {
					// Nothing here!

				}

				@Override
				public void onScrollingFinished(WheelView wheel) {
					d = digits[decimalGrade.getCurrentItem()];

				}
			});
			
////
			decimalGrade2.addChangingListener(new OnWheelChangedListener() {

				@Override
				public void onChanged(WheelView wheel, int oldValue,
						int newValue) {
					d2 = digits[decimalGrade2.getCurrentItem()];

				}
			});

			decimalGrade2.addScrollingListener(new OnWheelScrollListener() {

				@Override
				public void onScrollingStarted(WheelView wheel) {
					// Nothing here!

				}

				@Override
				public void onScrollingFinished(WheelView wheel) {
					d2 = digits[decimalGrade2.getCurrentItem()];

				}
			});
			

			percentageWheel.addChangingListener(new OnWheelChangedListener() {

				@Override
				public void onChanged(WheelView wheel, int oldValue,
						int newValue) {
					p = percentages[percentageWheel.getCurrentItem()];

				}
			});

			percentageWheel.addScrollingListener(new OnWheelScrollListener() {

				@Override
				public void onScrollingStarted(WheelView wheel) {
					// Nothing here!

				}

				@Override
				public void onScrollingFinished(WheelView wheel) {
					p = percentages[percentageWheel.getCurrentItem()];

				}
			});
		} else {
			Toast.makeText(getApplicationContext(),
					"Porcentaje maximo alcanzado", Toast.LENGTH_LONG).show();
			finish();
		}
	}

	/**
	 * Updates the decimal wheel
	 */
	public void updateDecimals(WheelView decimal, String decimals[][], int index) {
		ArrayWheelAdapter<String> adapter = new ArrayWheelAdapter<String>(this,
				decimals[index]);
		adapter.setTextSize(18);
		decimal.setViewAdapter(adapter);
		decimal.setCurrentItem(decimals[index].length / 2);
	}

	public void returnSelectedWheels(View view) {
		Intent resultIntent = new Intent(this, MainActivity.class);

		resultIntent.putExtra(GRADE_RESULT_FROM_DIALOG, i + d + d2);
		resultIntent.putExtra(PERCENTAGE_RESULT_FROM_DIALOG, p);
		setResult(Activity.RESULT_OK, resultIntent);
		finish();

	}
}
